package com.fruit.web.vo.goods.detail;

import java.awt.Dimension;

import java.io.IOException;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.faces.application.FacesMessage;
import javax.faces.application.ViewHandler;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.SessionScoped;
import javax.faces.bean.ViewScoped;
import javax.faces.component.UIViewRoot;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.ValueChangeEvent;
import javax.faces.model.SelectItem;
import javax.servlet.http.HttpServletRequest;

import net.sf.cglib.beans.BeanCopier;

import com.fruit.common.config.ConfigManager;
import com.fruit.controller.LoginController;
import com.fruit.service.IColorService;
import com.fruit.service.IConsultationService;
import com.fruit.service.ICustomerService;
import com.fruit.service.IGoodsService;
import com.fruit.utils.DESPlusUtil;
import com.fruit.utils.DateTimeUtil;
import com.fruit.utils.MessageFactory;
import com.fruit.utils.SendHtmlMail;
import com.fruit.utils.SortManagerDelegate;
import com.fruit.utils.SpringFacesUtil;
import com.fruit.utils.WebPageData;
import com.fruit.web.bo.impl.Color;
import com.fruit.web.bo.impl.Consultation;
import com.fruit.web.bo.impl.Customer;
import com.fruit.web.bo.impl.Goods;
import com.fruit.web.constant.EjdConstants;
import com.fruit.web.constant.ManageBeanConstants;
import com.fruit.web.constant.SpringBeanConstants;
import com.fruit.web.vo.common.ColorVo;
import com.fruit.web.vo.common.ConsultationPage;
import com.fruit.web.vo.common.ConsultationVo;
import com.fruit.web.vo.common.CustomerVo;
import com.fruit.web.vo.common.GoodsVo;
import com.fruit.web.vo.customer.LoginCustomerPageBean;
import com.fruit.web.vo.genl.GenerateVerifyCodeBean;
@ManagedBean(name="goodsDetailPage")
@ViewScoped

public class GoodsDetailPageBean implements java.io.Serializable {
	private static final long serialVersionUID = -8106629465344224713L;
	private String primaryCategoryCode;
	private String code;
	private String productId;
	private GoodsVo goods;
	private Integer buyNumber;
	private ConsultationPage aPage;
	private ConsultationPage gPage;
	private ConsultationPage lPage;
	private ConsultationPage pPage;
	private ConsultationVo consultation= new ConsultationVo();
	private String verifyCodeForQuestion;
	private String verifyCode;
	private String saveOutMessage;
	
	private String viewTab;

	public String getPrimaryCategoryCode() {
		return primaryCategoryCode;
	}
	public void setPrimaryCategoryCode(String primaryCategoryCode) {
		this.primaryCategoryCode = primaryCategoryCode;
	}
	public String getCode() {
		return code;
	}
	public void setCode(String code) {
		this.code = code;
	}
	
	public String getProductId() {
		return productId;
	}
	public void setProductId(String productId) {
		this.productId = productId;
	}
	public GoodsVo getGoods() {
		return goods;
	}
	public void setGoods(GoodsVo goods) {
		this.goods = goods;
	}
	
	public ConsultationPage getaPage() {
		return aPage;
	}
	public void setaPage(ConsultationPage aPage) {
		this.aPage = aPage;
	}
	public ConsultationPage getgPage() {
		return gPage;
	}
	public void setgPage(ConsultationPage gPage) {
		this.gPage = gPage;
	}
	public ConsultationPage getlPage() {
		return lPage;
	}
	public void setlPage(ConsultationPage lPage) {
		this.lPage = lPage;
	}
	public ConsultationPage getpPage() {
		return pPage;
	}
	public void setpPage(ConsultationPage pPage) {
		this.pPage = pPage;
	}
	
	public ConsultationVo getConsultation() {
		return consultation;
	}
	public void setConsultation(ConsultationVo consultation) {
		this.consultation = consultation;
	}
	
	public String getVerifyCodeForQuestion() {
		return verifyCodeForQuestion;
	}
	public void setVerifyCodeForQuestion(String verifyCodeForQuestion) {
		this.verifyCodeForQuestion = verifyCodeForQuestion;
	}
	
	public String getVerifyCode() {
		return verifyCode;
	}
	public void setVerifyCode(String verifyCode) {
		this.verifyCode = verifyCode;
	}
	
	public String getSaveOutMessage() {
		return saveOutMessage;
	}
	public void setSaveOutMessage(String saveOutMessage) {
		this.saveOutMessage = saveOutMessage;
	}
	
	public String getViewTab() {
		return viewTab;
	}
	public void setViewTab(String viewTab) {
		this.viewTab = viewTab;
	}
	
	public Integer getBuyNumber() {
		return buyNumber;
	}
	public void setBuyNumber(Integer buyNumber) {
		this.buyNumber = buyNumber;
	}
	
	public String getShowImageName() {
		String result = "noImage.jpg";
		if (null !=this.getGoods().getImageName() && !("".equals(this.getGoods().getImageName()))) {
			result = this.getGoods().getImageName();
		}
		return result;
	}
	
	public GoodsDetailPageBean() throws IOException {
		
		String primaryCategoryCode = (String) SpringFacesUtil.getRequestParameter("primaryCategoryCode");
		primaryCategoryCode = DESPlusUtil.decrypt(primaryCategoryCode);
		String code = (String) SpringFacesUtil.getRequestParameter("code");
		code = DESPlusUtil.decrypt(code);
		String productId = (String) SpringFacesUtil.getRequestParameter("productId");
		productId = DESPlusUtil.decrypt(productId);
		String sessionId = (String) SpringFacesUtil.getRequestParameter("sessionId");
		String currentSessionId = SpringFacesUtil.getCurrentSessionId();
		if (null == sessionId || "".equals(sessionId) || null == currentSessionId || "".equals(currentSessionId) || !(sessionId.equals(currentSessionId))) {
			SpringFacesUtil.redirectPage(EjdConstants.INDEX_HTML);
		}
		if (null == primaryCategoryCode || "".equals(primaryCategoryCode) || "null".equals(primaryCategoryCode)
				||null == code || "".equals(code) || "null".equals(code)
						|| null == productId || "".equals(productId) || "null".equals(productId)) {
			SpringFacesUtil.redirectPage(EjdConstants.INDEX_HTML);
		} else {
			this.setPrimaryCategoryCode(primaryCategoryCode);
			this.setCode(code);
			this.setProductId(productId);
			this.setBuyNumber(1);
			IGoodsService<Goods,Long> goodsService = (IGoodsService<Goods,Long>)SpringFacesUtil.getSpringBean(SpringBeanConstants.GOODS_SERVICE_BEAN_NAME);
			goodsService.setEntityClass(Goods.class);
			goodsService.setPKClass(Long.class);
			Goods currGoods = goodsService.findUniqueBy("code", code);
			BeanCopier copyGoods = BeanCopier.create(Goods.class, GoodsVo.class, false);
			GoodsVo goodsVo = new GoodsVo();
			copyGoods.copy(currGoods, goodsVo, null);
			this.setGoods(goodsVo);
			//end 
			// GenerateVerifyCode
			GenerateVerifyCodeBean generateVerifyCode = (GenerateVerifyCodeBean) SpringFacesUtil.getManagedBean(ManageBeanConstants.GENERATE_VERIFY_CODE_BEAN);
			this.setVerifyCodeForQuestion(generateVerifyCode.getVerifyCode());
			// end generateVerifyCode
			//init aPage
			Consultation paraA = new Consultation();
			paraA.setCode(code);
			paraA.setStatusCode("O");
			paraA.setShowOnPage(true);
			aPage = new ConsultationPage(1,5,paraA);
			aPage.getPageDatas();
			//end aPage
			//start gPage
			Consultation paraG = new Consultation();
			paraG.setCode(code);
			paraG.setTypeCode("G");
			paraG.setStatusCode("O");
			paraG.setShowOnPage(true);
			gPage  = new ConsultationPage(1,5,paraG);
			gPage.getPageDatas();
			//end gPage
			//start lpage
			Consultation paraL = new Consultation();
			paraL.setCode(code);
			paraL.setTypeCode("L");
			paraL.setStatusCode("O");
			paraL.setShowOnPage(true);
			lPage  = new ConsultationPage(1,5,paraL);
			lPage.getPageDatas();
			//end lpage
			//start ppage
			Consultation paraP = new Consultation();
			paraP.setCode(code);
			paraP.setTypeCode("P");
			paraP.setStatusCode("O");
			paraP.setShowOnPage(true);
			pPage  = new ConsultationPage(1,5,paraP);
			pPage.getPageDatas();
			//end ppage
			this.setViewTab(EjdConstants.CONSULTATION_VIEW_TAB_ALL);
		}
	}
	
	public void goDetailPage(ActionEvent evt) {
		String code =(String) SpringFacesUtil.getRequestParameter("code");
		this.setCode(code);
	}
	/*public String goDetailPage(){
		if (this.checkJspExist(this.getCode()+".xhtml", "/layout/goods/shoes") && this.checkJspExist(this.getCode()+"Detail.xhtml", "/layout/goods/shoes/sub") ) {
			return this.getCode() + "?faces-redirect=true&primaryCategoryCode=" + DESPlusUtil.encrypt(this.getPrimaryCategoryCode()) + "&code=" + DESPlusUtil.encrypt(this.getCode()) + "&productId=" + DESPlusUtil.encrypt(this.getProductId());
		} else {
			return "/WEB-INF/" + EjdConstants.NO_PRODUCT_DETAIL_PAGE;
		}
	}*/
	
	public void pageNoChangeOfgPage(ValueChangeEvent event) {
		String newValue = (String) event.getNewValue().toString();
		this.getgPage().setPageNo(Integer.valueOf(newValue));
		System.out.println(newValue);
		this.getgPage().getPageDatas();
	}
	public String selectaPageNo() {
		String result = null;
		String pageNo = (String) SpringFacesUtil.getRequestParameter("pageNo");
		if (this.getaPage().getPageNo().intValue() == Integer.valueOf(pageNo).intValue()) {
			return result;
		}
		this.getaPage().setPageNo(Integer.valueOf(pageNo).intValue());
		this.getaPage().getPageDatas();
		return result;
		
	}
	public String selectgPageNo() {
		String result = null;
		String pageNo = (String) SpringFacesUtil.getRequestParameter("pageNo");
		if (this.getgPage().getPageNo().intValue() == Integer.valueOf(pageNo).intValue()) {
			return result;
		}
		this.getgPage().setPageNo(Integer.valueOf(pageNo).intValue());
		this.getgPage().getPageDatas();
		return result;
		
	}
	public String selectlPageNo() {
		String result = null;
		String pageNo = (String) SpringFacesUtil.getRequestParameter("pageNo");
		if (this.getlPage().getPageNo().intValue() == Integer.valueOf(pageNo).intValue()) {
			return result;
		}
		this.getlPage().setPageNo(Integer.valueOf(pageNo).intValue());
		this.getlPage().getPageDatas();
		return result;
		
	}
	public String selectpPageNo() {
		String result = null;
		String pageNo = (String) SpringFacesUtil.getRequestParameter("pageNo");
		if (this.getpPage().getPageNo().intValue() == Integer.valueOf(pageNo).intValue()) {
			return result;
		}
		this.getpPage().setPageNo(Integer.valueOf(pageNo).intValue());
		this.getpPage().getPageDatas();
		return result;
		
	}
	public String generateVerifyCode() {
		GenerateVerifyCodeBean generateVerifyCode = (GenerateVerifyCodeBean) SpringFacesUtil.getManagedBean(ManageBeanConstants.GENERATE_VERIFY_CODE_BEAN);
		this.setVerifyCodeForQuestion(generateVerifyCode.getVerifyCode());
		return null;
	}
	public String saveConsultation(){
		this.setNullOfSaveOutMessage();
		String code = (String)SpringFacesUtil.getRequestParameter("code");
		String name = (String)SpringFacesUtil.getRequestParameter("name");
		String primaryCategoryCode = (String)SpringFacesUtil.getRequestParameter("primaryCategoryCode");
		String secondCategoryCode = (String)SpringFacesUtil.getRequestParameter("secondCategoryCode");
		String productId = (String)SpringFacesUtil.getRequestParameter("productId");
		String statusCode = EjdConstants.CONSULTATION_STATUS_CODE_NEW;
		boolean showOnPage = false;
		if (!(LoginController.loggedIn())) {
			return EjdConstants.GO_TO_LOGIN + "?faces-redirect=true";
		}
		Consultation newConsultation = new Consultation();
		this.getConsultation().setCode(code);
		this.getConsultation().setName(name);
		this.getConsultation().setPrimaryCategoryCode(primaryCategoryCode);
		this.getConsultation().setSecondCategoryCode(secondCategoryCode);
		this.getConsultation().setProductId(new Long(productId).longValue());
		this.getConsultation().setStatusCode(statusCode);
		this.getConsultation().setShowOnPage(showOnPage);
		BeanCopier copyHere = BeanCopier.create(ConsultationVo.class,Consultation.class,false);
		copyHere.copy(this.getConsultation(), newConsultation, null);
		Customer customer = new Customer();
		/*ICustomerService<Customer,Long> customerService = (ICustomerService<Customer,Long>) SpringFacesUtil.getSpringBean(SpringBeanConstants.CUSTOMER_SERVICE_BEAN_NAME);
		customerService.setEntityClass(Customer.class);
		customerService.setPKClass(Long.class);*/
		LoginCustomerPageBean loginCustomerPage = (LoginCustomerPageBean)SpringFacesUtil.getManagedBean(ManageBeanConstants.LOGIN_CUSTOMER_PAGE_BEAN_NAME);
		customer = loginCustomerPage.getCustomerInfo();
		newConsultation.setCustomer(customer);
		newConsultation.setAskTime(DateTimeUtil.getCurrentTimestamp());
		IConsultationService<Consultation,Long> consultationService = (IConsultationService<Consultation,Long>) SpringFacesUtil.getSpringBean(SpringBeanConstants.CONSULTATION_SERVICE_BEAN_NAME);
		consultationService.setEntityClass(Consultation.class);
		consultationService.setPKClass(Long.class);
		try{
			consultationService.save(newConsultation);
			FacesMessage message;
			message = MessageFactory.getMessage(EjdConstants.USER_BUNDLE_NAME, "consultation.save.success.out.message", FacesMessage.SEVERITY_ERROR, customer.getName());
			this.setSaveOutMessage(message.getDetail());
		} catch (Exception e) {
			FacesMessage message;
			message = MessageFactory.getMessage(EjdConstants.USER_BUNDLE_NAME, "consultation.save.fail.out.message", FacesMessage.SEVERITY_ERROR, customer.getName());
			this.setSaveOutMessage(message.getDetail());
            throw new InternalError(
                " Runtime Exception impossibility throw ");
        }
		GenerateVerifyCodeBean generateVerifyCode = (GenerateVerifyCodeBean) SpringFacesUtil.getManagedBean(ManageBeanConstants.GENERATE_VERIFY_CODE_BEAN);
		this.setVerifyCodeForQuestion(generateVerifyCode.getVerifyCode());
		return null;
	}
	private void setNullOfSaveOutMessage(){
		this.setSaveOutMessage(null);
	}
	public String selectTab(){
		String tabNumber = (String)SpringFacesUtil.getRequestParameter("tabNumber");
		System.out.println(tabNumber);
		return null;
	}
	public String selectConsultationViewTab(){
		String consultationViewTab = (String)SpringFacesUtil.getRequestParameter("consultationViewTab");
		if (null != consultationViewTab && !("".equals(consultationViewTab))) {
			this.setViewTab(consultationViewTab);
		} else {
			this.setViewTab(EjdConstants.CONSULTATION_VIEW_TAB_ALL);
		}
		return null;
	}
	
	public String shareToFriend(String primaryCategoryCode, String code, String id) throws Exception {
		primaryCategoryCode = DESPlusUtil.encrypt(primaryCategoryCode);
		code = DESPlusUtil.encrypt(code);
		id = DESPlusUtil.encrypt(id);
		ConfigManager configer = ConfigManager.getInstance();
		String website = configer.getProperty("web.site");
		boolean useProxy = new Boolean(configer.getProperty("url.useProxy")).booleanValue();
	    String urlHost = configer.getProperty("url.host");
	    int urlPort = new Integer(configer.getProperty("url.port")).intValue();
	    String urlUsername = configer.getProperty("url.username");
	    String urlPassword = configer.getProperty("url.password");
		String urlContent = "";
		WebPageData webPage = new WebPageData(useProxy,urlPort,urlHost,urlUsername,urlPassword);
		String viewId = SpringFacesUtil.getViewId();
		FacesContext fc = SpringFacesUtil.getFacesContext();
		String urlStr = website + fc.getExternalContext().getRequestContextPath() + viewId + "?" + "primaryCategoryCode=" + primaryCategoryCode + "&code=" + code + "&productId=" + id;
		urlContent = webPage.sendUrl(urlStr, null);
		String mailTo = "jqhero79@gmail.com";
		String mailFrom = configer.getProperty("email.from");
		String smtpServer = configer.getProperty("email.host");
		String mailPort = configer.getProperty("email.port");
		String mailProxySet = configer.getProperty("email.proxySet");
		String mailSocksProxyHost = configer.getProperty("email.socksProxyHost");
		String mailSocksProxyPort = configer.getProperty("email.socksProxyPort");
		String mailUserName = configer.getProperty("email.username");
		String mailPassword = configer.getProperty("email.password");
		String mailSubject = configer.getProperty("email.subject");
		//SendHtmlMail sendHtmlMail = new SendHtmlMail(mailTo,mailFrom,smtpServer,mailPort,mailProxySet,mailSocksProxyHost,mailSocksProxyPort,mailUserName,mailPassword,mailSubject,urlContent);
		//sendHtmlMail.sendMail();
		
		return null;
	}
}
